package com.njupt.DynamicProgramming;

public class WeightBagProblem {
    public int eightBagProblem(int[] weight,int[] value,int bagSize){

        int goods = weight.length;
        int[][] dp = new int[goods][bagSize+1];

        //初始化DP数组
        for (int i = 0; i < goods; i++) {
            dp[i][0] = 0;
        }
        for (int j = weight[0]; j <bagSize ; j++) {
            dp[0][j] = value[0];
        }

        //从前往后遍历DP数组
        for (int i = 1; i < goods; i++) {
            for (int j = 1; j <= bagSize; j++) {

                if(j<weight[i]){
                    dp[i][j] = dp[i-1][j];
                }else {
                    dp[i][j] = Math.max(dp[i-1][j] , dp[i-1][j-weight[i]] + value[i]);
                }

            }
        }

        //打印DP数组
        for (int i = 0; i < goods; i++) {
            for (int j = 0; j <=bagSize ; j++) {
                System.out.print(dp[i][j] +" ");
            }
            System.out.println();
        }
        System.out.println();
        return dp[goods-1][bagSize];
    }

    public static void main(String[] args) {
        WeightBagProblem test = new WeightBagProblem();
        int[] weight = {2,2,3,1,5,2};
        int[] value = {2,3,1,5,4,3};
        int re = test.eightBagProblem(weight, value, 1);
        System.out.println(re);
    }
}
